/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.loof.fonzie.parsers;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

public class EntityMetaDataRepository
{
    private final static Comparator<Field> FIELD_COMPARATOR = new Comparator<Field>()
    {
        public int compare( Field f1, Field f2 )
        {
            // longest name win
            return f2.getName().length() - f1.getName().length();
        }
    };

    @SuppressWarnings( "unchecked" )
    public static String entityName( Class entity )
    {
        Entity annotation = (Entity) entity.getAnnotation( Entity.class );
        String name = annotation.name();
        if (name.length() == 0)
        {
            name = entity.getSimpleName();
        }
        return name;
    }

    @SuppressWarnings( "unchecked" )
    private static ConcurrentMap<Class, List<Field>> attributesCache = new ConcurrentHashMap<Class, List<Field>>();

    @SuppressWarnings( "unchecked" )
    protected static List<Field> readAttributes( Class entity )
    {

        List<Field> attributes = attributesCache.get( entity );
        if ( attributes != null )
        {
            return attributes;
        }
        attributes = doReadAttributes( entity );
        attributesCache.putIfAbsent( entity, attributes );
        return attributes;
    }

    @SuppressWarnings( "unchecked" )
    private static List<Field> doReadAttributes( Class entity )
    {
        List<Field> attributes;
        attributes = new LinkedList<Field>();
        Class searchType = entity;
        while ( searchType.getAnnotation( Entity.class ) != null
            || searchType.getAnnotation( MappedSuperclass.class ) != null )
        {
            Field[] fields = searchType.getDeclaredFields();
            for ( int i = 0; i < fields.length; i++ )
            {
                Field field = fields[i];
                if ( Modifier.isTransient( field.getModifiers() ) || field.getAnnotation( Transient.class ) != null )
                {
                    continue;
                }
                attributes.add( field );
            }
            searchType = searchType.getSuperclass();
        }

        Collections.sort( attributes, FIELD_COMPARATOR );
        return attributes;
    }
}
